package com.yc.cloud.common.message.sms.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;

import com.yc.cloud.common.message.sms.config.SmsMobileProperties;
import com.yc.cloud.common.message.sms.dto.request.SendMobileResultDto;
import com.yc.cloud.common.message.sms.service.SmsService;
import com.yc.cloud.common.message.sms.dto.request.SmsMobileCodeParam;
import com.yc.cloud.common.message.sms.dto.request.SmsMobileParam;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Service;


@Service
@Slf4j
public class SmsServiceImpl implements SmsService {


    @Value("${spring.mail.username}")
    private String from;

    @Resource
    private SmsMobileProperties smsMobileProperties;

//    @Resource
//    private SmsCacheService smsCacheService;


    @Override
    public SendMobileResultDto sendMobile(SmsMobileParam param) {
        Credential cred = new Credential(smsMobileProperties.getId(), smsMobileProperties.getKey());
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        SmsClient client = new SmsClient(cred, "ap-shanghai", clientProfile);
        SendSmsRequest req = new SendSmsRequest();
        req.setSmsSdkAppid(smsMobileProperties.getAppId());
        req.setSign(smsMobileProperties.getSign());
        req.setTemplateID(param.getTemplateId());
        String[] phoneNumbers = {smsMobileProperties.getCoutry() + param.getTo()};

        // String[] phoneNumbers = {param.getUsername()};
        req.setPhoneNumberSet(phoneNumbers);
        // String[] templateParams = {param.getContent(), smsMobileProperties.getValidTime()};
        req.setTemplateParamSet(param.getTemplateParams());
        SendSmsResponse res;
        try {
            res = client.SendSms(req);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            return new SendMobileResultDto(false, e.getMessage());
        }
        SendStatus sendStatus = res.getSendStatusSet()[0];
        String smsCode = sendStatus.getCode();
        if ("Ok".equals(smsCode)) {
            return new SendMobileResultDto(true, "短信发送成功");
        }
        log.error("短信发送失败:" + smsCode);
        // Asserts.fail(getSMSErrorMsg(smsCode));
        return new SendMobileResultDto(false, getSMSErrorMsg(smsCode));
    }

    private String getSMSErrorMsg(String smsCode) {
        if ("LimitExceeded.PhoneNumberDailyLimit".equals(smsCode)) {
            return "抱歉,今日发送验证码的次数达到上限";
        }
        return "短信发送失败,原因为:" + smsCode;
    }

//    @Override
//    public CommonResponseResult<String> sendMobileCode(SmsMobileCodeParam param) {
//        val smsMobileParam = new SmsMobileParam();
//        smsMobileParam.setTo(param.getMobile());
//        val code = RandomUtil.randomNumbers(6);
//        //单位分钟
//        val validTime = smsMobileProperties.getValidTime();
//        val templateParams = new String[]{code, validTime};
//        smsMobileParam.setTemplateParams(templateParams);
//        smsMobileParam.setTemplateId(smsMobileProperties.getTemplateId());
//        val sendMobileResult = sendMobile(smsMobileParam);
//        //将验证码信息存放到缓存中
//        if (sendMobileResult.getCode() == CommonResponseResult.success().getCode()) {
//            //redis set 采用单位秒,所以需要将分钟转换成秒
//            val validTimeSecond = Long.parseLong(validTime) * 60;
//            smsCacheService.set(param.getMobile(), param.getType(), code, validTimeSecond);
//        }
//        return sendMobileResult;
//    }

    public static void main(String[] args) {
        val code = RandomUtil.randomNumbers(6);
        System.out.println(code);
    }
}
